package com.jk.controller;

import com.alibaba.fastjson.JSON;
import com.jk.api.EsServiceApi;
import com.jk.bo.EsDestinationBO;
import com.jk.dao.EsRepository;
import com.jk.service.EsService;
import com.jk.vo.EsDestinationVO;
import org.elasticsearch.action.search.SearchRequestBuilder;
import org.elasticsearch.client.Client;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightField;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.stereotype.Controller;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

@Controller
public class EsController implements EsServiceApi {
    @Autowired
    private EsService esService;
    @Autowired
    private EsRepository esRepository;
    @Autowired
    private ElasticsearchTemplate elasticsearchTemplate;
    /**
    * @Author  郑滨
    * @method  queryDestination
    * @description 描述一下方法的作用
    * @Date 19:41 2021/7/7
    * @Param  * @param
    * @return java.util.List<com.jk.vo.EsDestinationVO>
    **/
    @Override
    public List<EsDestinationVO> queryDestination() {
        EsDestinationBO esDestinationBO = new EsDestinationBO();
        List<EsDestinationVO> EsDestinationVOS = esService.queryDestination();
        for (EsDestinationVO x : EsDestinationVOS) {
            BeanUtils.copyProperties(x,esDestinationBO);
            esRepository.save(esDestinationBO);
        }
        return EsDestinationVOS;
    }
    /**
    * @Author  郑滨
    * @method  queryDestinationES
    * @description 描述一下方法的作用
    * @Date 10:04 2021/7/8
    * @Param  * @param search
    * @return java.util.List<com.jk.bo.EsDestinationBO>
    **/
    @Override
    public List<EsDestinationBO> queryDestinationES(EsDestinationBO search) {
        //获取es的操作客户端
        Client client = elasticsearchTemplate.getClient();
        //创建查询构造器
        SearchRequestBuilder searchRequestBuilder = client.prepareSearch("destination").setTypes("sanzu");
        //设置查询的字段   matchQuery 第一个参数查询的字段 第二参数为内容
        searchRequestBuilder.setQuery(QueryBuilders.matchQuery("aname",search.getAname()));
        HighlightBuilder highlightBuilder = new HighlightBuilder();
        //设置高亮字段
        highlightBuilder.field("aname");
        //设置高亮开始标签
        highlightBuilder.preTags("<font color='red'>");
        //设置高亮结束标签
        highlightBuilder.postTags("</font>");
        //把高亮信息放入查询构造器
        searchRequestBuilder.highlighter(highlightBuilder);
        //获取到执行结果
        SearchHits hits = searchRequestBuilder.get().getHits();
        List<EsDestinationBO> productList = new ArrayList<>();
        //获取到查询迭代器
        Iterator<SearchHit> iterator = hits.iterator();
        while (iterator.hasNext()){
            //有下一个数据时 获取到下一个数据的内容
            SearchHit searchResult = iterator.next();
            //获取到查询结果的json字符串
            String productJSON = searchResult.getSourceAsString();
            EsDestinationBO productBO = JSON.parseObject(productJSON, EsDestinationBO.class);
            //替换高亮结果
            Map<String, HighlightField> highlightFields = searchResult.getHighlightFields();
            HighlightField productName = highlightFields.get("aname");
            String productNameResult = productName.fragments()[0].string();
            productBO.setAname(productNameResult);
            //把查询结果放入list结合中
            productList.add(productBO);
        }
        return productList;
    }
}
